const fs = require('fs');
const xlsx = require('node-xlsx');
const readline = require('readline');
const request = require("request");
var path = require("path");
var url = path.resolve('./');

const https = require('https');
interface flies {
    flieName: string;
}


class flie {

    //判断文件夹 或文件是否存在
    static judgeFlie(url: string, cb) {
        fs.exists(url, (exists) => {
            cb(exists)
        })
    }

    //创建文件
    static txt(de: flies) {
        const { flieName } = de;
        this.judgeFlie(`${flieName}.txt`, (e: boolean) => {
            if (!e) {
                fs.writeFile(`${flieName}.txt`, '', function (err: object) {
                    if (err) {
                        console.log(err)
                        return console.log(err);
                    }
                    console.log(`创建问价：${flieName}成功`);
                });
            }
        })
    }

    //创建文件夹
    static folder(de: flies) {
        const { flieName } = de;
        this.judgeFlie(flie + `${flieName}.txt`, (e: boolean) => {
            if (!e) {
                fs.mkdir(flie + flieName, (err: any) => {
                    err ? console.error(`创建文件：${flie}失败！${err}`) : console.log(`创建文件：${flie}成功！`);
                })

            }
        })
    }

}


// interface write {
//     url: string;
//     data: string;
// }

// class writeIn {  //写入txt文件
//     static writetxt(datas: write) {
//         const { url, data } = datas;
//         flie.judgeFlie(url, (e) => {
//             console.log(e)
//             // if (!e) {
//             //     flie.txt({ flieName: url })

//             // } else {
//                 // console.log(data)
//                 fs.appendFile(`../${url}`, "" + data + "\n", 'utf8', function (err: object) {
//                     // console.log(err);
//                     // err && console.log(err);

//                 });
//             // }

//         })

//     }
// }


interface rendr {
    url: string;
    cb: Function;
}

class FileToRead {
    /**
     * 
     * @param url 读取文件路径
     * @param cb  读取的回调函数
     */
    static getxlsx(url, cb) {  //读取xlsx文件
        const obj = xlsx.parse(url);
        const excelObj = obj[0].data;
        for (let i = 0; i < excelObj.length; i++) {
            cb(excelObj[i])
        }
    }

    static getTxt(data: rendr) { //读取txt文件
        const { url, cb } = data;
        const fli = readline.createInterface({
            input: fs.createReadStream(url)
        });
        fli.on('line', (line) => {
            cb(line)
        })


    }
}

class ImgesWrite {
    constructor() {

    }


  static  img(fileName, src){
    let sm = request.get(src).on('response', function (response) {
        let [, suffix] = response.headers['content-type'].split('/');
        if (response.statusCode === 200) {
            sm.pipe(fs.createWriteStream(`${url}/${fileName}/${new Date().getTime()}.${suffix}`))
        }
    })

    }
}

// 
// ImgesWrite.img('imgs','blob:https://ffxx99.com/021caf79-695c-4e95-bc5f-b6bfe4d65d54')


// FileToRead.getxlsx('../../dataCont/工作簿1.xlsx',e=>{
//     console.log(e)
// })


// https.globalAgent.on('keylog', (line, tlsSocket) => {
//     console.log(line)
//     console.log(tlsSocket)

//     fs.appendFileSync('./ssl-keys.log', line, { mode: 0o600 });
//   });

// https.get('blob:https://ffxx99.com/021caf79-695c-4e95-bc5f-b6bfe4d65d54', (res) => {
//   console.log('状态码:', res.statusCode);
//   console.log('请求头:', res.headers);

//   res.on('data', (d) => {
//     process.stdout.write(d);
//   });

// }).on('error', (e) => {
//   console.error(e);
// });



let ImgesWrites = ImgesWrite.img

export { flie, FileToRead , ImgesWrites}

// FileToRead.getTxt({url:'../../dataCont/onlinePicture.txt',cb:e=>{
//     console.log(e)
// }})